const express = require('express');
const pool = require('../util/pool.js');
const r = express.Router();
const myfs = require("fs");
const selfFun = require('../util/selfFun.js');
//1 引入

// URL:http://127.0.0.1:8080/v1/maps/upload
r.post('/upload', (req, res, next) => {
    let img = req.body;
    console.log(img);
    let x, sta = 400;
    for (x in img) {
        if (!img[x]) { //x属性名， obj[x]属性值
            res.send({
                code: sta,
                msg: x + '不能为空'
            });
            //阻止往后执行
            return;
        }
        sta++;
    }
    pool.query('insert into xz_index_carousel set ?', [img], (err, rusults) => {
        if (err) {
            next(err);
            return;
        }
        res.send({
            code: 200,
            msg: '上传成功'
        });
    });
});

// URL:http://127.0.0.1:8080/v1/maps/delete 6
r.delete('/delete:cid', (req, res, next) => {
    let id = req.params.cid;
    console.log(req.params);
    pool.query('delete from xz_index_carousel where cid=?', [id], (err, results) => {
        //   console.log(results);
        if (err) {
            next(err);
            return;
        }
        if (results.affectedRows === 0) {
            res.send({
                code: 404,
                msg: '删除失败'
            });
        } else {
            res.send({
                code: 200,
                msg: '删除成功'
            });
        }
    });
});

// URL:http://127.0.0.1:8080/v1/maps/update
r.put('/update', (req, res, next) => {
    let obj = req.body;
    let x, sta = 400;
    // console.log(obj);
    // 循环验证
    for (x in obj) {
        if (!obj[x]) { //x属性名， obj[x]属性值
            res.send({
                code: sta,
                msg: x + '不能为空'
            });
            //阻止往后执行
            return;
        }
        sta++;
    }

    pool.query('update xz_index_carousel set ? where cid=?', [obj, obj.cid], (err, results) => {
        if (err) {
            next(err);
            return;
        }
        if (results.changedRows === 0) {
            res.send({
                code: 201,
                msg: '修改失败！'
            });
            return;
        }
        res.send({
            code: 200,
            msg: '修改成功'
        });
    });
});

r.get('/list', (req, res, next) => {
    pool.query('select * from xz_index_carousel', (err, results) => {
        if (err) {
            next(err);
            return;
        }
        res.send(results);
    });
});


r.get('/find:cid', (req, res, next) => {
    pool.query('select * from xz_index_carousel where cid=?', [req.params.cid], (err, results) => {
        if (err) {
            next(err);
            return;
        }
        res.send(results);
    });
});

r.post("/upimg", (req, res, next) => {
    /*     console.log(req.files); //文件上传请求信息
        console.log(req.query.uid); */
    let tempPath = req.files[0].path; //找到临时路径
    var obj = req.files[0].fieldname;
    let cid = req.query.cid;
    let filecode;
    if (!cid) {
        res.send({
            code: 201,
            msg: '上传失败！'
        });
        return;
    }
    // selfFun.getSuffix(obj)得到上传文件后缀
    // console.log('selfFun.getSuffix(obj): ', selfFun.getSuffix(obj));
    let filename = "banner" + cid + '.' + selfFun.getSuffix(obj);
    let targetPath = "views/imgStoreHouse/carouselMap/" + filename; //将文件放入上传上来的目标路径
    console.log('targetPath: ', targetPath);
    if (myfs.existsSync("./views/imgStoreHouse/carouselMap/") == false) { //判断upload路径下是否存在该用户的文件夹
        myfs.mkdirSync("./views/imgStoreHouse/carouselMap/"); //没有的话，就创建一个文件夹
    }
    // 上传图片
    myfs.rename(tempPath, targetPath, function (err, data) {
        if (err) {
            next(err);
            return;
        }
        // 检查图片是否上传成功
        if (!myfs.existsSync(targetPath)) {
            console.log('!myfs.existsSync(targetPath): ', !myfs.existsSync(targetPath));
            res.send({
                code: 201,
                msg: '上传失败！'
            });
            return;
        }
    });
    //   上传成功后，向数据库中插入路径
    let src="imgStoreHouse/carouselMap/"+filename;
    pool.query('update xz_index_carousel set img=?,href=? where cid=?', [targetPath,src,cid], (err, results) => {
        // console.log('results: ', results);
        if (err) {
            next(err);
            return;
        }
        if (results.affectedRows) {
            res.send({
                code: 200,
                msg: '上传成功！'
            });
        }
    });
});

module.exports = r;